﻿using Carer.Crm.Utility;
using Carer.Data;
using Carer.Data.Cache;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Carer.Crm.Services
{
    public sealed class UserService : BaseService
    {
        private static Logger _logger = LogManager.GetLogger("UnitService");

        public User CheckUser(string name, string pass)
        {
            string sSql = @"SELECT [User].[Id]
                  ,[User].[Name]
                  ,[User].[Password]
                  ,[User].[Role]
                  ,[User].[Revoke]
                  ,[User].[Remark]
                  ,[User].[UnitId]
                  ,[Unit].[Id] AS UnitId
                  ,[Unit].[Name] AS UnitName
                  ,[Unit].[Addr] AS UnitAddr
                  ,[Unit].[Order] AS UnitOrder
                  ,[Unit].[Kind] AS UnitKind
                  ,[Unit].[Remark] AS UnitRemark
                  ,[Unit].[Company] AS UnitCompany
              FROM [dbo].[User]
              LEFT OUTER JOIN [dbo].[Unit] ON [Unit].Id=[User].UnitId
              WHERE 1=1";


            List<SqlParameter> paramList = new List<SqlParameter>();
            if (!string.IsNullOrWhiteSpace(name))
            {
                sSql += " AND [User].[Name]=@name";
                paramList.Add(new SqlParameter()
                {
                    SqlDbType = SqlDbType.VarChar,
                    ParameterName = "name",
                    Value = name
                });
            }
            if (!string.IsNullOrWhiteSpace(pass))
            {
                sSql += " AND [User].[Password]=@pass";
                paramList.Add(new SqlParameter()
                {
                    SqlDbType = SqlDbType.VarChar,
                    ParameterName = "pass",
                    Value = pass
                });
            }
            User result = null;
            try
            {
                DataTable dt = SqlHelper.ExecuteDataTable(this.CONN_STR, System.Data.CommandType.Text, sSql, paramList.ToArray());
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];
                    result = new User()
                    {
                        Id = row["Id"].ToGetValue(0),
                        Name = row["Name"].ToGetValue(string.Empty),
                        Password = row["Password"].ToGetValue(string.Empty),
                        Role = row["Role"].ToGetValue(0),
                        Revoke = row["Revoke"].ToGetValue(false),
                        Remark = row["Remark"].ToGetValue(string.Empty),
                        UnitId = row["UnitId"].ToGetValue(0),
                        UnitName = row["UnitName"].ToGetValue(string.Empty),
                        Unit = row["UnitId"].ToGetValue(0) == 0 ? null : new Unit()
                        {
                            Id = row["UnitId"].ToGetValue(0),
                            Name = row["UnitName"].ToGetValue(string.Empty),
                            Addr = row["UnitAddr"].ToGetValue(string.Empty),
                            Order = row["UnitOrder"].ToGetValue(0),
                            Kind = row["UnitKind"].ToGetValue(string.Empty),
                            Remark = row["UnitRemark"].ToGetValue(string.Empty),
                            Company = row["UnitCompany"].ToGetValue(string.Empty)
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "GetAllPayments报错:");
            }

            return result;
        }
    }
}
